﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace BTVPodcatcherNamespace
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Process instance = RunningInstance();
            if (instance == null)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainForm());
            }
            else
            {
                showProcessWindow(instance);
            }
        }
        [DllImport("User32.dll")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("User32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);

        [DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        private const int WS_SHOWNORMAL = 1;
        private const int SW_HIDE = 0;
        private const int SW_MAX = 10;
        private const int SW_MAXIMIZE = 3;
        private const int SW_MINIMIZE = 6;
        private const int SW_NORMAL = 1;
        private const int SW_RESTORE = 9;
        private const int SW_SHOW = 5;
        private const int SW_SHOWDEFAULT = 10;
        private const int SW_SHOWMAXIMIZED = 3;
        private const int SW_SHOWMINIMIZED = 2;
        private const int SW_SHOWMINNOACTIVE = 7;
        private const int SW_SHOWNA = 8;
        private const int SW_SHOWNOACTIVATE = 4;
        private const int SW_SHOWNORMAL = 1;

        private static void showProcessWindow(Process instance)
        {
            try
            {
                IntPtr winHandel = instance.MainWindowHandle;
                if (winHandel.Equals(IntPtr.Zero))
                {
                    winHandel =FindWindow(null,"BTV Podcatcher");
                }
                if (!winHandel.Equals(IntPtr.Zero))
                {
                    ShowWindowAsync(winHandel, WS_SHOWNORMAL);
                    ShowWindowAsync(winHandel, SW_RESTORE);
                    ShowWindowAsync(winHandel, SW_SHOW);


                    //Set the real intance to foreground window
                    SetForegroundWindow(winHandel);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

        }


        public static Process RunningInstance()
        {
            Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(current.ProcessName);

            //Loop through the running processes in with the same name
            foreach (Process process in processes)
            {
                //Ignore the current process
                if (process.Id != current.Id)
                {
                    //Make sure that the process is running from the exe file.
                    //if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
                    {
                        //Return the other process instance.
                        return process;
                    }
                }
            }

            //No other instance was found, return null.
            return null;
        }
    }
}
